package me.zhengjie.controller.test;

import me.zhengjie.dao.StoreLocalDao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


// 对应数据库表的实体类
// 数据生成器
public class WarehouseDataGenerator {
    private static final int TOTAL_RECORDS = 100000;  // 生成10万行数据
    private static final String[] FLOORS = {"M1", "M2", "M3", "M4"};  // 仓库号
    private static final String[] STORE_TYPES = {"托盘储位", "料箱储位"};  // 储位类型
    private static final String[] WARE_TYPES = {"原料", "辅料", "半成品"};  // 物品类型
    private static final String[] GOOD_TYPES = {"普通货架", "线边储位", "抽检区", "分拣区", "理货区"};  // 货位类型
    private static final String[] REGION_NAMES = {"原料区", "辅料区", "半成品区", "待检区", "成品区"};  // 区域名称

    // 分布比例（百分比）
    private static final int PALLET_RACK_PERCENT = 60;  // 托盘储位占比
    private static final int RAW_MATERIAL_PERCENT = 50;  // 原料占比
    private static final int ORDINARY_RACK_PERCENT = 60;  // 普通货架占比
    private static final int CLOSE_TO_DOOR_PERCENT = 30;  // 靠近门口占比
    private static final int LOCKED_PERCENT = 15;  // 锁定状态占比

    // 状态权重分布（百分比）
    private static final int STATUS_EMPTY_PERCENT = 40;  // 空闲状态占比
    private static final int STATUS_FULL_PERCENT = 35;  // 有货状态占比
    private static final int STATUS_INBOUND_PERCENT = 12;  // 入库状态占比
    // 出库状态占比 = 100% - 空闲 - 有货 - 入库

    private static final Random random = new Random();

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        System.out.println("开始生成数据，预计生成 " + TOTAL_RECORDS + " 行...");

        // 生成数据并存储到List
        List<StoreLocalDao> locationList = generateWarehouseLocations(TOTAL_RECORDS);

        long endTime = System.currentTimeMillis();
        System.out.printf("数据生成完成，耗时: %.2f秒，共生成%,d行数据%n",
                (endTime - startTime) / 1000.0, locationList.size());

        // 打印前10条数据示例
        System.out.println("\n前10条数据示例：");
        for (int i = 0; i < Math.min(10, locationList.size()); i++) {
            System.out.println(locationList.get(i));
        }

        // TODO: 这里可以将数据写入数据库或其他存储
        // saveToDatabase(locationList);
    }

    public static List<StoreLocalDao> generateWarehouseLocations(int count) {
        List<StoreLocalDao> locations = new ArrayList<>(count);

        for (int i = 0; i < count; i++) {
            locations.add(generateWarehouseLocation());

            // 每10000行打印进度
            if ((i + 1) % 10000 == 0) {
                System.out.printf("已生成%,d行数据，进度: %.2f%%%n",
                        i + 1, (i + 1.0) / count * 100);
            }
        }

        return locations;
    }

    private static StoreLocalDao generateWarehouseLocation() {
        StoreLocalDao location = new StoreLocalDao();
        Timestamp now = new Timestamp(System.currentTimeMillis());

        // 1. 生成基础信息
        String floor = getRandomFloor();
        String region = String.format("%02d", random.nextInt(5) + 1);  // 区域号01-05
        boolean isPallet = random.nextInt(100) < PALLET_RACK_PERCENT;
        String storeType = isPallet ? STORE_TYPES[0] : STORE_TYPES[1];

        // 2. 生成储位编号和名称
        String frame = String.format("%02d", isPallet ? random.nextInt(8) + 1 : random.nextInt(15) + 1);  // 层数
        String serial = String.format("%03d", random.nextInt(31) + 20);  // 储位号020-050
        String id = String.format("%s_A%s_L%s_P%s", floor, region, frame, serial);
        String regionName = REGION_NAMES[Integer.parseInt(region) - 1];
        String storeName = String.format("仓库%s-%s-%s层-%s号",
                floor, regionName, frame, serial);

        // 3. 生成类型信息
        String wareType = getRandomWareType();
        String goodType = getRandomGoodType();

        // 4. 生成载具码
        String containerCode = String.format("VT%s%04d",
                isPallet ? "TP" : "BC", random.nextInt(9000) + 1000);

        // 5. 生成状态信息
        int status = getRandomStatus();
        int isLock = random.nextInt(100) < LOCKED_PERCENT ? 1 : 0;

        // 6. 生成尺寸和重量
        double weight = isPallet ?
                random.nextDouble() * 1500 + 500 :  // 托盘500-2000kg
                random.nextDouble() * 90 + 10;     // 料箱10-100kg

        double sLong = isPallet ?
                random.nextDouble() * 50 + 100 :  // 托盘100-150cm
                random.nextDouble() * 30 + 30;    // 料箱30-60cm

        double sWide = isPallet ?
                random.nextDouble() * 40 + 80 :   // 托盘80-120cm
                random.nextDouble() * 30 + 20;    // 料箱20-50cm

        double sTall = isPallet ?
                random.nextDouble() * 150 + 150 : // 托盘150-300cm
                random.nextDouble() * 25 + 15;    // 料箱15-40cm

        // 7. 设置实体类属性
        location.setId(id);
        location.setStoreName(storeName);
        location.setFloor(floor);
        location.setRegion(region);
        location.setFrame(frame);
        location.setSerial(serial);
        location.setWareType(wareType);
        location.setStoreType(storeType);
        location.setGoodType(goodType);
        location.setStatus(status);
        location.setIsLock(isLock);
        location.setContainerCode(containerCode);
        location.setCreateDate(now);
        location.setUpdateDate(now);
        location.setWeight(weight);
        location.setSLong(sLong);
        location.setSWide(sWide);
        location.setSTall(sTall);

        return location;
    }

    private static String getRandomFloor() {
        return FLOORS[random.nextInt(FLOORS.length)];
    }

    private static String getRandomWareType() {
        int rand = random.nextInt(100);
        if (rand < RAW_MATERIAL_PERCENT) {
            return WARE_TYPES[0];  // 原料
        } else if (rand < RAW_MATERIAL_PERCENT + 30) {
            return WARE_TYPES[1];  // 辅料
        } else {
            return WARE_TYPES[2];  // 半成品
        }
    }

    private static String getRandomGoodType() {
        int rand = random.nextInt(100);
        if (rand < ORDINARY_RACK_PERCENT) {
            return GOOD_TYPES[0];  // 普通货架
        } else if (rand < ORDINARY_RACK_PERCENT + 15) {
            return GOOD_TYPES[1];  // 线边储位
        } else if (rand < ORDINARY_RACK_PERCENT + 25) {
            return GOOD_TYPES[2];  // 抽检区
        } else if (rand < ORDINARY_RACK_PERCENT + 35) {
            return GOOD_TYPES[3];  // 分拣区
        } else {
            return GOOD_TYPES[4];  // 理货区
        }
    }

    private static int getRandomStatus() {
        int rand = random.nextInt(100);
        if (rand < STATUS_EMPTY_PERCENT) {
            return 0;  // 空闲
        } else if (rand < STATUS_EMPTY_PERCENT + STATUS_FULL_PERCENT) {
            return 3;  // 有货
        } else if (rand < STATUS_EMPTY_PERCENT + STATUS_FULL_PERCENT + STATUS_INBOUND_PERCENT) {
            return 1;  // 入库
        } else {
            return 2;  // 出库
        }
    }
}


